﻿#region Copyright (c) 2013-04, Olaf Kober <amarok.projects@gmail.com>
//================================================================================
//	Permission is hereby granted, free of charge, to any person obtaining a copy
//	of this software and associated documentation files (the "Software"), to deal
//	in the Software without restriction, including without limitation the rights
//	to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
//	copies of the Software, and to permit persons to whom the Software is
//	furnished to do so, subject to the following conditions:
//
//	The above copyright notice and this permission notice shall be included in
//	all copies or substantial portions of the Software.
//
//	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
//	IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
//	FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//	AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
//	LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
//	OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
//	THE SOFTWARE.
//================================================================================
#endregion

using System;


namespace Amarok.Agents
{
	/// <summary>
	/// This static class provides various extension methods for <see cref="IAdvertiser"/>.
	/// </summary>
	public static class AdvertiserExtensions
	{
		#region Subscribe<T>(AgentReference)

		/// <summary>
		/// Subscribes the supplied agent on the publisher. All messages published that match or derive 
		/// from the specified message type and that pass the optional message filter will be forwarded 
		/// to the subscriber.
		/// </summary>
		/// 
		/// <typeparam name="TMessage">
		/// The base type of messages for which to subscribe. This type must be assignable to <see cref="Message"/>.</typeparam>
		/// 
		/// <param name="advertiser">
		/// The advertiser instance on which this method is called.</param>
		/// <param name="subscriber">
		/// An agent reference to the agent subscribing for messages.</param>
		/// <param name="messageFilter">
		/// A callback used as content-based filter. Optional, defaults to null.</param>
		/// 
		/// <returns>
		/// An object representing the subscription that can be used to unsubscribe from the publisher, 
		/// or a null value when the advertiser has already be completed.</returns>
		/// 
		/// <exception cref="ArgumentNullException">
		/// A null reference was passed to a method that did not accept it as a valid argument.</exception>
		public static ICompletable Subscribe<TMessage>(
			this IAdvertiser advertiser,
			AgentReference subscriber,
			Predicate<TMessage> messageFilter = null)
			where TMessage : Message
		{
			Verify.NotNull(subscriber, "subscriber");

			Predicate<Message> baseTypeMessageFilter = messageFilter == null ?
				(Predicate<Message>)null :
				_message => messageFilter((TMessage)_message);

			return advertiser.Subscribe(
				typeof(TMessage),
				subscriber,
				baseTypeMessageFilter);
		}

		#endregion

		#region Subscribe<T>(Action<T>)

		/// <summary>
		/// Subscribes the supplied callback on the publisher. All messages published that match or derive 
		/// from the specified message type and that pass the optional message filter will be forwarded 
		/// to the subscriber.
		/// </summary>
		/// 
		/// <typeparam name="TMessage">
		/// The base type of messages for which to subscribe. This type must be assignable to <see cref="Message"/>.</typeparam>
		/// 
		/// <param name="advertiser">
		/// The advertiser instance on which this method is called.</param>
		/// <param name="subscriber">
		/// A callback subscribing for messages.</param>
		/// <param name="messageFilter">
		/// A callback used as content-based filter. Optional, defaults to null.</param>
		/// 
		/// <returns>
		/// An object representing the subscription that can be used to unsubscribe from the publisher, 
		/// or a null value when the advertiser has already be completed.
		/// </returns>
		/// 
		/// <exception cref="ArgumentNullException">
		/// A null reference was passed to a method that did not accept it as a valid argument.</exception>
		public static ICompletable Subscribe<TMessage>(
			this IAdvertiser advertiser,
			Action<TMessage> subscriber,
			Predicate<TMessage> messageFilter = null)
			where TMessage : Message
		{
			Verify.NotNull(subscriber, "subscriber");

			Predicate<Message> baseTypeMessageFilter = messageFilter == null ?
				(Predicate<Message>)null :
				_message => messageFilter((TMessage)_message);

			Action<Message> baseTypeCallback =
				_message => subscriber((TMessage)_message);

			return advertiser.Subscribe(
				typeof(TMessage),
				baseTypeCallback,
				baseTypeMessageFilter);
		}

		#endregion

	}
}
